package org.jin.samples.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.jin.samples.dao.IManageStudentDao;
import org.jin.samples.dao.ISubjectDao;
import org.jin.samples.model.ExamSubject;
import org.jin.samples.model.ExamUser;
import org.jin.samples.model.vo.ExamSubjectVO;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.service.ISubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SubjectServiceImpl implements ISubjectService{
	
	@Autowired
	private ISubjectDao subjectDao;
	
	@Autowired
	private IManageStudentDao manageStudentDao;
	
	public List<ExamSubject> findAllSubject(){
		return subjectDao.find("FROM ExamSubject");
	}

	@Override
	public ExamSubjectVO findSubjectById(Integer id) {
		ExamSubject examSubject =  subjectDao.get(id);
		ExamSubjectVO vo = new ExamSubjectVO();
		BeanUtils.copyProperties(examSubject,vo);
		return vo;
	}

	@Override
	public void addSubject(ExamSubjectVO examSubjectVO) {
		ExamSubject examSubject = new ExamSubject();
		BeanUtils.copyProperties(examSubjectVO,examSubject);
		subjectDao.save(examSubject);
	}

	@Override
	public void Updata(ExamSubjectVO examSubjectVO) {	
		ExamSubject ed = subjectDao.get(examSubjectVO.getSubId());
		BeanUtils.copyProperties(examSubjectVO, ed);
		System.out.println("service: ed.getStatus()="+ed.getStatus());
		if(ed != null){
			BeanUtils.copyProperties(examSubjectVO, ed);
			subjectDao.saveOrUpdate(ed);
		}
	}

	@Override
	public boolean deleteSubject(Integer subId){ 
		ExamSubject ed = subjectDao.get(subId);
		if(ed != null){
			if(ed.getStatus()==0){
			subjectDao.deleteByKey(subId);
			return true;
			}
		}
		return false;
	}
	
	
	@Override
	public void UpdataStatus(ExamSubjectVO examSubjectVO) {	
		ExamSubject ed = subjectDao.get(examSubjectVO.getSubId());
		ed.setStatus(examSubjectVO.getStatus());
		if(ed != null){
			subjectDao.saveOrUpdate(ed);
		}
	}

	@Override
	public List<String> getAllUserBuSubjectId(Integer subId)throws Exception{
		return subjectDao.getAllUserBySubjectId(subId);
	}
	
	@Override
	public List<ExamUserVO> findAllStudent(){
		List<ExamUser> examUserList = manageStudentDao.find("FROM ExamUser");
		List<ExamUserVO> examUserVOList = new ArrayList<>();
		for(ExamUser user : examUserList){
			ExamUserVO examUserVO = new ExamUserVO();
			BeanUtils.copyProperties(user, examUserVO);
			examUserVOList.add(examUserVO);	
		}
		return examUserVOList;
	}
	
	public void saveSubjectStudent(List<String> subjectStudents, Integer subjectId)throws Exception{
		List<String> dbSubjectUserList = subjectDao.getAllUserBySubjectId(subjectId);//获取当前科目
		List<String> insertList = new ArrayList<>();
		List<String> delList = new ArrayList<String>();
		
		//1有 2无 的情况
		//判断哪些学生是新勾选上的
		for(String selectedUser : subjectStudents){
			if(!dbSubjectUserList.contains(selectedUser)){
				insertList.add(selectedUser);
			}
		}
		
		//1无 2有的情况
		//判断哪些学生本来就是勾选上的，现在去掉勾选上的
		for(String selectedUser : dbSubjectUserList){
			if(!subjectStudents.contains(selectedUser)){
				delList.add(selectedUser);
			}
		}
		
		//insertList 分别保存
		subjectDao.insertSubjectUsers(insertList, subjectId);
		//delList fe分别删除
		subjectDao.deleteSubjectUsers(delList, subjectId);	
	}

	@Override
	public ExamSubject getSubjectById(int sub_id) {
		return 	subjectDao.get(sub_id);
	}
	
	
}
